// pages/fenxiao/index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rootUrl: rootDocment,
    hiddenLoading: false,
    userInfo: {},
    fenxiao: {
      now_money: 0,
      yesterday_money: 0.00,
      new_money: 0.00,
      yesterday_order: 0,
      yesterday_user: 0,
      settled_order: 0,
      all_money: 0.00
    },
    showPic: false,
    sharePic: '',
    shopName: wx.getStorageSync('owner_shop_name')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopName: wx.getStorageSync('owner_shop_name')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //用户授权登录
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
      that.getFenxiaoInfo();
    });
  },

  /**
   * 获取会员分销信息
   */
  getFenxiaoInfo: function () {
    var that = this;
    if (that.data.userInfo.isFX) {
      var paraArr = new Array();
      paraArr['user_id'] = that.data.userInfo.userID;
      var sign = app.signature(paraArr);
      wx.request({
        url: rootDocment + '/api/fenxiao/getFenxiaoSurvey',
        data: {
          user_id: paraArr['user_id'],
          sign: sign
        },
        method: 'GET',
        header: {},
        success: function (res) {
          that.setData({
            'fenxiao.now_money': res.data.nowMoney,
            'fenxiao.yesterday_money': res.data.yesterdayMoney,
            'fenxiao.new_money': res.data.newMoney,
            'fenxiao.yesterday_order': res.data.yesterdayOrder,
            'fenxiao.yesterday_user': res.data.yesterdayUser,
            'fenxiao.settled_order': res.data.settledOrder,
            'fenxiao.all_money': res.data.allMoney,
            hiddenLoading: true
          });
        }
      })
    }
  },
  //获取分享图
  getQrcode: function () {
    var that = this;
    if (that.data.showPic) {
      that.setData({
        showPic: !that.data.showPic
      });
      return
    }
    if (that.data.shopName == '') {
      wx.showModal({
        title: '',
        content: '请先设置店铺名称',
        success(res) {
          if (res.confirm) {
            wx.showModal({
              editable: true,
              title: '修改店铺名称',
              content: that.data.shopName,
              complete: (res) => {
                if (res.confirm) {
                  if (res.content == '') {
                    wx.showToast({
                      title: '请输入名称',
                      icon: 'none',
                    })
                    return
                  }
                  that.setData({
                    shopName: res.content
                  })
                  var paraArr = new Array();
                  paraArr['user_id'] = wx.getStorageSync('user_id3');
                  paraArr['shop_name'] = res.content;
                  var sign = app.signature(paraArr);
                  wx.request({
                    url: rootDocment + '/api/llgn/updateShopName',
                    data: {
                      user_id: paraArr['user_id'],
                      shop_name: paraArr['shop_name'],
                      sign: sign
                    },
                    method: 'POST',
                    header: {},
                    success: function (res) {
                      if (res.data.code == 1001) {
                        wx.setStorageSync('owner_shop_name',res.content);
                        wx.showToast({
                          title: '修改成功',
                          icon: 'none',
                        })
                      }
                    }
                  });
                }
              }
            });
          } 
        }
      });
    } else {
      if (that.data.showPic) {
        that.setData({
          showPic: !that.data.showPic
        });
      } else {
        that.setData({
          showShare: false,
          hiddenLoading: false
        });
        var m_page = 'pages/index/index';
        var m_scene = that.data.userInfo.userID;
        var paraArr = new Array();
        paraArr['page'] = m_page;
        paraArr['scene'] = m_scene;
        var sign = app.signature(paraArr);
        wx.request({
          url: rootDocment + '/api/get_qrcode/myQrcode',
          data: {
            page: m_page,
            scene: m_scene,
            sign: sign
          },
          method: 'GET',
          header: {},
          success: function (res) {
            console.log(res)
            if (res.data.success == true) {
              that.setData({
                showPic: !that.data.showPic,
                sharePic: res.data.msg + '?' + Math.random(),
                hiddenLoading: true
              });
            }else{
              that.setData({
                hiddenLoading: true
              });
              wx.showToast({
                title: res.data.msg,
                icon:'none'
              })
            }
          }
        })
      }
    }
    
  },
  //保存分享图片
  savePic: function () {
    var that = this;
    wx.downloadFile({
      url: that.data.rootUrl + that.data.sharePic,
      success: function (res) {
        let path = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success(res) {
            wx.showToast({
              title: '保存成功'
            })
          },
          fail(res) {
            wx.showToast({
              title: '保存失败'
            })
          },
          complete(res) {
            that.setData({
              showPic: false
            });
          }
        })
      },
      fail: function (res) {
        console.log(res)
      }
    })

  },
  // 修改店铺名称
  setShopName() {
    let t = this
    wx.showModal({
      editable: true,
      title: '修改店铺名称',
      content: t.data.shopName,
      complete: (res) => {
        if (res.confirm) {
          if (res.content == '') {
            wx.showToast({
              title: '请输入名称',
              icon: 'none',
            })
            return
          }
          t.setData({
            shopName: res.content
          })
          var paraArr = new Array();
          paraArr['user_id'] = wx.getStorageSync('user_id3');
          paraArr['shop_name'] = res.content;
          var sign = app.signature(paraArr);
          wx.request({
            url: rootDocment + '/api/llgn/updateShopName',
            data: {
              user_id: paraArr['user_id'],
              shop_name: paraArr['shop_name'],
              sign: sign
            },
            method: 'POST',
            header: {},
            success: function (res) {
              if (res.data.code == 1001) {
                wx.setStorageSync('owner_shop_name',res.content);
                wx.showToast({
                  title: '修改成功',
                  icon: 'none',
                })
              }

            }
          })

        }
      }
    })
  },
  /**
   * 佣金转余额
   * showModal 弹窗
   */
  getall_money() {
    let t = this
    wx.showModal({
      title: `拥有佣金${this.data.fenxiao.now_money}`,
      editable: true,
      placeholderText: '请输入金额',
      success(res) {
        if (res.confirm) {
          console.log(res.content, '用户点击确定')
          var paraArr = new Array();
          paraArr['user_id'] = wx.getStorageSync('user_id3');
          paraArr['money'] = res.content;
          var sign = app.signature(paraArr);
          wx.request({
            url: rootDocment + '/api/llgn/commissionToBalance',
            data: {
              user_id: paraArr['user_id'],
              money: paraArr['money'],
              // sign: sign
            },
            method: 'POST',
            header: {},
            success: function (res) {
              if (res.data.code == 1001) {
                wx.showToast({
                  title: '成功',
                  icon: 'none',
                  duration: 2000
                })
                t.getFenxiaoInfo()
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    app.globalData.userInfo.userID = '';
    //用户授权登录
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
      that.getFenxiaoInfo();
    });
    wx.stopPullDownRefresh();
  }

})